home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiniExamples / AppKit / ConvertXYtoChar / Controller.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  128 lines

  1. /* Controller.m
  2.  * Purpose:  A subclass of Object and the main controlling class of
  3.  *    the application.  Controller is the application delegate.
  4.  *
  5.  * You may freely copy, distribute, and reuse the code in this example.
  6.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  7.  * fitness for any particular use.
  8.  *
  9.  * Written by: Sharon Zakhour
  10.  * Created: 22/July/91
  11.  *
  12.  */
  13.  
  14. #import <stdio.h>
  15. #import "TurboText.h"
  16. #import "Controller.h"
  17.  
  18. @implementation Controller
  19.  
  20. /* Since we have subclassed the Text object but we have used */
  21. /* The ScrollView from IB we need to replace the Text object in  */
  22. /* the IB-generated ScrollView with our own subclass.  At the */
  23. /* same time we want to preserve the text that was inserted into */
  24. /* the view.  The replaceText method performs this swap.  This */
  25. /* is also covered in the NeXTanswer  appkit.555. */
  26. - appDidInit:sender
  27. {
  28.     /* Replace the Text object in the ScrollView with my */
  29.     /* own TurboText object */
  30.     [self replaceText];
  31.     
  32.     /* Now that we're ready, bring the window up */
  33.     /* In IB, we've instructed the window NOT to become */
  34.     /* visible at launch time so that we can make this swap */
  35.     /* invisibly. */
  36.     [[myScrollView window] makeKeyAndOrderFront: nil];
  37.     return self;
  38. }
  39.  
  40. /* This method is called when the user selects the ConvertTo */
  41. /* button in the window.  The x,y coordinates are extracted from */
  42. /* two form fields and the result is placed into yet-another */
  43. /* form field.  At the same time, the character position is echoed */
  44. /* in the ScrollView with an insertion cursor at the new position. */
  45. - convert:sender
  46. {
  47.     NXPoint    myPoint;
  48.     int    charPos;
  49.     NXSelPt    start, end;
  50.     NXRect  myRect;
  51.     
  52.     myPoint.x = [[charCoordinate cellAt: 0:0] floatValue];
  53.     myPoint.y = [[charCoordinate cellAt: 1:0] floatValue];
  54.     
  55.     charPos = [[myScrollView docView] convertPoint: &myPoint];
  56.     [[charPosition cellAt: 0: 0] setFloatValue: (float)charPos];
  57.     
  58.     /* Set the selection at the character position so that */
  59.     /* we can see it.  This will also make the ScrollView the */
  60.     /* first responder which is necessary to see the blinking */
  61.     /* insertion point. */
  62.     [[myScrollView docView] setSel: charPos :charPos];
  63.     
  64.     /* Now let's get the selection point back.  The actual x,y */
  65.     /* coordinates have been adjusted by the Text object */
  66.     /* and we want to update our textfields */
  67.     [[myScrollView docView] getSel: &start :&end];
  68.  
  69.     /* scroll to the new location */
  70.     myRect.origin.x = start.x;
  71.     myRect.origin.y = start.y;
  72.     myRect.size.width = 1.0;
  73.     myRect.size.height = 20.0;
  74.     [[myScrollView docView] scrollRectToVisible:&myRect];
  75.  
  76.     [[charPosition cellAt: 1:0] setFloatValue: start.x];
  77.     [[charPosition cellAt: 2:0] setFloatValue: start.y];
  78.     return self;
  79. }
  80.  
  81. /* Swaps a standard Text object from IB with my own.  See */
  82. /* NeXTanswer appkit.555 for more information. */
  83. - replaceText
  84. {
  85.     NXStream *s = (NXStream *)nil;
  86.     id        stdDoc, newDoc;
  87.     NXRect    r;
  88.         
  89.     /* Measure the old doc view, then trash it. */
  90.     stdDoc = [myScrollView docView];
  91.     [stdDoc getFrame:&r];
  92.  
  93.     /* Get the entire text, using the rich RTF format */
  94.     s = NXOpenMemory(NULL, 0, NX_READWRITE);
  95.     if (s)   [stdDoc writeRichText: s];
  96.  
  97.     [myScrollView setVertScrollerRequired:YES];
  98.     [myScrollView setHorizScrollerRequired:NO];
  99.     [myScrollView setDynamicScrolling:YES]; 
  100.     
  101.     newDoc = [[TurboText alloc] initFrame:&r];
  102.     [newDoc moveTo:0.0:0.0];
  103.     [newDoc notifyAncestorWhenFrameChanged: YES];
  104.     [newDoc setVertResizable:YES];
  105.     [newDoc setSelectable:YES];
  106.     [newDoc setEditable: YES];
  107.     [newDoc setAutosizing:NX_WIDTHSIZABLE];
  108.     [newDoc setMinSize:&r.size];
  109.     [newDoc setMonoFont:NO];
  110.     r.size.height = 1.0e30;
  111.     [newDoc setMaxSize:&r.size];
  112.     [myScrollView setDocView:newDoc];
  113.     [stdDoc free];
  114.     
  115.     /* Stick the text from the original doc into the new doc */
  116.     if (s)
  117.     {
  118.         /* Rewind to the beginning of the stream */
  119.         NXSeek(s, 0L, NX_FROMSTART);
  120.         [newDoc readRichText:s];
  121.         NXCloseMemory(s, NX_FREEBUFFER);
  122.     }
  123.  
  124.     return self;
  125. }
  126.  
  127. @end
  128.